﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CosView.GUI
{
    public class Drawer
    {
        public Drawer()
        {
            Invalidate();
            /*SomeMethod sm = new SomeMethod(Invalidate);
            sm.BeginInvoke(null, null);*/
        }

        public delegate void SomeMethod();

        public static List<DrawerObject> Objects = new List<DrawerObject>();
        public static void AddObject(DrawerObject obj)
        {
            obj.Changed += new DrawerObject.EmptyDelegate(obj_Changed);
            Objects.Add(obj);
            Invalidate();
        }
        private static void obj_Changed()
        {
            Invalidate();
        }
        public static void RemObject(DrawerObject obj)
        {
            Objects.Remove(obj);
            Invalidate();
        }
        public static ConsoleColor Background = ConsoleColor.Black;
        public static void Init()
        {
            ClearScreen();
            Running = true;
        }
        public static void Stop()
        {
            Running = false;
        }
        private static bool Running = false;
        private static void AInvalidate()
        {
            while (true)
            {
                if (Running)
                    Invalidate();
            }
        }
        public static void Invalidate()
        {
            // Backup data
            ConsoleColor consoleback = Console.BackgroundColor;
            ConsoleColor consolefore = Console.ForegroundColor;
            /*int CursorTop = Console.CursorTop;
            int CursorLeft = Console.CursorLeft;*/
            /*bool CursorVisible = Console.CursorVisible;
            Console.CursorVisible = false;*/

            // Draw background
            Console.BackgroundColor = Background;
            Console.Clear();
            
            // Draw a black background
            ClearScreen();

            // Draw
            for (int i = 0; i < Objects.Count; i++)
            {
                if (Objects[i].Type == ObjectType.Object)
                {
                    Console.BackgroundColor = Objects[i].BackColor;
                    Console.ForegroundColor = Objects[i].BackColor;
                    for (int y = Objects[i].StartY; y < Objects[i].EndY; y++ )
                    {
                        Console.CursorTop = y;
                        Console.CursorLeft = Objects[i].StartX;
                        for (int x = Objects[i].StartX; x < Objects[i].EndX; x++)
                        {
                            Console.Write('#');
                        }
                    }
                    Console.CursorLeft = Objects[i].StartX + 1;
                    Console.CursorTop = Objects[i].StartY;
                    Console.ForegroundColor = Objects[i].ForeColor;
                    for (int l = 0; (l < (Objects[i].EndX - Objects[i].StartX) - 2) && (l < Objects[i].Text.Length); l++)
                    {
                        Console.Write(Objects[i].Text[l]);
                    }
                }
            }

            // Restore data
            Console.BackgroundColor = consoleback;
            Console.ForegroundColor = consolefore;
            //Console.CursorLeft = CursorLeft;
            //Console.CursorTop = CursorTop;
            Console.WriteLine(Environment.NewLine);
            //Console.WriteLine(Environment.NewLine);
            //Console.CursorVisible = CursorVisible;
        }
        private static void ClearScreen()
        {
            ConsoleColor bc = Console.BackgroundColor;
            ConsoleColor fc = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Black;
            Console.BackgroundColor = ConsoleColor.Black;

            for (int x = 0; x < Console.WindowWidth; x++ )
            {
                for (int y = 0; y < Console.WindowHeight; y++)
                {
                    Console.CursorLeft = x;
                    Console.CursorTop = y;
                    Console.Write('#');
                }
            }

            Console.BackgroundColor = bc;
            Console.ForegroundColor = fc;
        }
    }

    public enum ObjectType
    {
        Object,
        Text
    }

    public class DrawerObject
    {
        /*public int ScreenWidth = 0;
        public int ScreenHeight = 0;
        public int StartX = 0;
        public int StartY = 0;
        public int EndX = 0;
        public int EndY = 0;*/
        private int _scw = Console.WindowWidth;
        private int _sch = Console.WindowHeight;
        private int _ssx = 0;
        private int _ssy = 0;
        private int _sex = 0;
        private int _sey = 0;
        private ConsoleColor _fc = ConsoleColor.White;
        private ConsoleColor _bc = ConsoleColor.Blue;
        public string UID = "";
        private string _tex = "";
        public string Text { get { return _tex; } set { _tex = value; if (Changed != null) Changed(); } }
        //public int ScreenWidth { get { return _scw; } set { _scw = value; if (Changed != null) Changed(); } }
        //public int ScreenHeight { get { return _sch; } set { _sch = value; if (Changed != null) Changed(); } }
        public int StartX { get { return _ssx; } set { _ssx = value; if (Changed != null) Changed(); } }
        public int StartY { get { return _ssy; } set { _ssy = value; if (Changed != null) Changed(); } }
        public int EndX { get { return _sex; } set { _sex = value; if (Changed != null) Changed(); } }
        public int EndY { get { return _sey; } set { _sey = value; if (Changed != null) Changed(); } }
        public virtual ConsoleColor BackColor { get { return _bc; } set { _bc = value; } }
        public virtual ConsoleColor ForeColor { get { return _fc; } set { _fc = value; } }
        public ObjectType Type = ObjectType.Object;
        public event EmptyDelegate Changed;
        public delegate void EmptyDelegate();
        public void Focus()
        {
            Console.CursorLeft = StartX + 1;
            Console.CursorTop = StartY;
        }
        public DrawerObject(int ex, int ey, int sx, int sy)
        {
            EndX = ex;
            EndY = ey;
            StartX = sx;
            StartY = sy;
        }
        public DrawerObject(int ex, int ey, int sx, int sy, ConsoleColor Background, ConsoleColor Foreground)
        {
            EndX = ex;
            EndY = ey;
            StartX = sx;
            StartY = sy;
            BackColor = Background;
            ForeColor = Foreground;
        }
    }
}
